package com.example.baselibrary.receivers

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.example.baselibrary.utlis.RestartHelper

class RestartReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent?) {
        when (intent?.action) {
            RestartHelper.ACTION_RESTART -> {
                val reason = intent.getStringExtra("REASON") ?: "UNKNOWN"
                // 可以根据不同原因执行不同逻辑
                when (reason) {
                    "USER_EXIT" -> RestartHelper.restartImmediately(context)
                    "CRASH" -> {
                        // 崩溃后延迟2秒重启
                        android.os.Handler(context.mainLooper).postDelayed({
                            RestartHelper.restartImmediately(context)
                        }, 2000)
                    }
                    else -> RestartHelper.restartImmediately(context)
                }
            }
        }
    }
}